package cn.icyzx.dao;

import cn.icyzx.models.*;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.SelectProvider;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author zk
 * @Date 2017/2/14.
 * @Email 2117251154@qq.com
 */
@Mapper
@Repository
public interface WorkMapper {

    @SelectProvider(type = SqlProvider.class, method = "getWorkByWid")
    @ResultMap("cyzx.mapper.work")
    Work getWorkByWid(String wid);

    @Select("select count(*) from work_store where uid=#{uid} and wid = #{wid} and type = 'collection'")
    int isCollection(@Param("wid") String wid, @Param("uid") String uid);

    @Select("select count(*) from work_store where uid = #{uid} and wid = #{wid} and type = 'recommend'")
    int isRecommend(@Param("wid") String wid, @Param("uid") String uid);

    /***
     *  getListWorks  返回workList的统一接口
     * @param condition
     * @return workList
     */
    @SelectProvider(type = SqlProvider.class, method = "getListWorks")
    @ResultMap("cyzx.mapper.work")
    List<Work> getListWorks(Map<String, String> condition);

    /*** 根据wid获取collectionNumber
     *
     * @param wid
     * @return number
     */
    @Select("select count(*) from work_store where wid = #{wid} and type = 'collection'")
    int getCollectionNumberByWid(@Param("wid") String wid);

    /*** 根据wid获取recommendNumber
     *
     * @param wid
     * @return number
     */
    @Select("select count(*) from work_store where wid = #{wid} and type = 'recommend'")
    int getRecommendNumberByWid(@Param("wid") String wid);

    /*** 根据wid获取overNumber
     *
     * @param wid
     * @return number
     */
    @Select("select count(*) from work_store where wid = #{wid} and type = 'over'")
    int getOverNumberByWid(@Param("wid") String wid);

    /*** 根据wid获取comments的数量
     *
     * @param wid
     * @return comments数量
     */
    @Select("select count(*) from comment where wid = #{wid}")
    int getlistCommentsNumberByWid(@Param("wid") String wid);

    @SelectProvider(type = SqlProvider.class, method = "getWorksSum")
    @ResultType(int.class)
    int getWorksSum(Map<String, String> condition);

    @Select("select * from work join user on (user.uid = work.uid) limit #{m},1")
    @ResultMap("cyzx.mapper.work")
    Work randomWorks(int m);

    @Select("select * from work join user on (user.uid = work.uid)")
    @ResultMap("cyzx.mapper.work")
    List<Work> allWorks();

    @Select("select * from cyzx_hotWork") //一周的热议作品
    @ResultMap("cyzx.mapper.work")
    List<Work> getHotWork();

    @SelectProvider(type = SqlProvider.class, method = "getListCommentsByWid")
    @ResultMap("cyzx.mapper.comment")
    List<Comment> getlistCommentsByWid(String wid);

    @Select("select count(*) from work where uid = #{uid}")
    int getWorkNumberByUid(@Param("uid") String uid);

    @Select("select count(*) from comment where uid = #{uid}")
    int getCommentsNumberByUid(@Param("uid") String uid);

    @Select("select count(*) from work_store where uid = #{uid} and type='collection'")
    int getCollectionNumberByUid(@Param("uid") String uid);

    @Delete("delete from work_store where wid = #{wid} and uid = #{uid} and type = #{type} ")
    int removeFromWorkStore(@Param("wid") String wid, @Param("uid") String uid, @Param("type") String type);

    @Select("select * from cyzx_tag where tagName = #{tag}")
    Tag getTagByName(@Param("tag") String tag);

    @Select("select * from cyzx_tag order by quoteNumber desc limit 0,10")
    List<Tag> getHotTags();

    @Insert("insert into tag(tagName) values(#{tag})")
    int addTag(@Param("tag") String tag);

    @Insert("insert into work_tag(wid,tag) values(#{wid},#{tag})")
    int addWorkTagRecord(@Param("wid") String wid, @Param("tag") String tag);

    /*** add 推荐和收藏
     *
     * @param wid
     * @param uid
     * @param type
     * @return
     */
    @Insert("insert into work_store(uid,wid,type) values(#{uid},#{wid},#{type})")
    int insertToWorkStore(@Param("wid") String wid, @Param("uid") String uid, @Param("type") String type);

    @Insert("insert into work(wid,uid,title,info,publicTime) values(#{wid},#{uid},#{name},#{info},#{publicTime})")
    int insertWork(Work work);

    @Update("update work set overNumber = overNumber + 1 where wid = #{wid}")
    int over(@Param("wid") String wid);

}
